/*                                                              *
 * Project name : "Meteor" engine                               *
 * Version      : 1.8                                           *
 * Date         : 2000                                          *
 *                                                              *
 * Copyright    : dReddi                                        *
 */

#include "cplayer.h"
#include "externals.h"

/*
 * CMyPlayer class
 */
M_int CMyPlayer::Init(M_ActorData ActorData)
    {
    // Init actor variables
    
    V_ActorVVector.x=0;
    V_ActorVVector.y=0;
    V_ActorVVector.z=0;
    V_ActorRVector.x=0;
    V_ActorRVector.y=0;
    V_ActorRVector.z=0;
    V_ActorDimX=20.0F;
    V_ActorDimY=70.0F;
    V_ActorDimZ=20.0F;
    V_ActorWeight=70.0F;
    V_ActorAllowXRotatedOBBox=FALSE;
    V_ActorAllowYRotatedOBBox=TRUE;
    V_ActorAllowZRotatedOBBox=FALSE;

    // Player physics default variables    
    
    PlayerMaxLookUpAngle=1.57F;
    PlayerMaxLookDownAngle=-1.57F;
    PlayerAccelerate=50.0F;    // j.odl/sec
    PlayerTurnSpeed=0.04F;
    PlayerJumpStrength=650.0F; // j.odl/sec

    // Player camera
    
    Camera.position=ActorData.position;
    Camera.orientation=ActorData.orientation;
    Camera.sector=ActorData.sector;
    
    return TRUE;
}

M_int CMyPlayer::Done(M_void)
    {
    return TRUE;
}

M_void CMyPlayer::Timer(M_void)
    {
    M_int mdx,mdy;
    M_float dimy_4;
    
    // Is physics active?   
    
    if (V_PausePhysics==FALSE)
        {

        // Read player keys   
        
        if (V_ActorEnvironment==envGround)
            {
            if (IsKeyDown(KeyMoveForward))
                {
                Move(THIS_ACTOR,0,V_ActorAy,0,PlayerAccelerate);
            }
            if (IsKeyDown(KeyMoveBackward))
                {
                Move(THIS_ACTOR,0,V_ActorAy,0,-PlayerAccelerate);
            }
            if (IsKeyDown(KeyStrafeLeft))
                {
                Strafe(THIS_ACTOR,0,V_ActorAy,0,-PlayerAccelerate);
            }
            if (IsKeyDown(KeyStrafeRight))
                {
                Strafe(THIS_ACTOR,0,V_ActorAy,0,PlayerAccelerate);
            }
            if (IsKeyDown(KeyJump))
                {
                Jump(THIS_ACTOR,PlayerJumpStrength);
            }
        }
    
        if (V_ActorEnvironment==envUnderWater)
            {
            if (IsKeyDown(KeyMoveForward))
                {
                Move(THIS_ACTOR,Camera.orientation.x,V_ActorAy,0,PlayerAccelerate);
            }
            if (IsKeyDown(KeyMoveBackward))
                {
                Move(THIS_ACTOR,Camera.orientation.x,V_ActorAy,0,-PlayerAccelerate);
            }
            if (IsKeyDown(KeyStrafeLeft))
                {
                Strafe(THIS_ACTOR,Camera.orientation.x,V_ActorAy,0,-PlayerAccelerate);
            }
            if (IsKeyDown(KeyStrafeRight))
                {
                Strafe(THIS_ACTOR,Camera.orientation.x,V_ActorAy,0,PlayerAccelerate);
            }
            if (IsKeyDown(KeyJump))
                {
                Jump(THIS_ACTOR,PlayerAccelerate);
            }
        }

        // Player head moving
        
        if (IsKeyDown(KeyTurnLeft))
            {
            Turn(THIS_ACTOR,0,-PlayerTurnSpeed,0);
        }
        if (IsKeyDown(KeyTurnRight))
            {
            Turn(THIS_ACTOR,0,PlayerTurnSpeed,0);
        }
        if (IsKeyDown(KeyLookUp))
            {
            Turn(THIS_ACTOR,PlayerTurnSpeed,0,0);
        }
        if (IsKeyDown(KeyLookDown))
            {
            Turn(THIS_ACTOR,-PlayerTurnSpeed,0,0);
        }

        // Mouse head moving
        
        GetMouseMove(mdx,mdy);
        if ((mdx!=0) || (mdy!=0))
            {
            Turn(THIS_ACTOR,(M_float)mdy*MousePitchSpeed,(M_float)mdx*MouseYawSpeed,0);
        }
        
        // Apply physics to player
        
        ApplyPhysics(THIS_ACTOR);
    
        // Update world camera ( FPP style,interpolate position )
        
        dimy_4=(V_ActorDimY*0.25F);

        Camera.position.x+=((V_ActorX+(V_ActorOBBOxOYVector.x*dimy_4))-Camera.position.x)*0.25F;
        Camera.position.y+=((V_ActorY+(V_ActorOBBOxOYVector.y*dimy_4))-Camera.position.y)*0.25F;
        Camera.position.z+=((V_ActorZ+(V_ActorOBBOxOYVector.z*dimy_4))-Camera.position.z)*0.25F;
        Camera.orientation.x=V_ActorRVector.x;
        Camera.orientation.y=V_ActorRVector.y;
        Camera.orientation.z=V_ActorRVector.z;
        Camera.sector=V_ActorSector;

        UpdateCameraFPPView(&Camera);
    }
}    
